package com.dmgameclient.common;

import android.annotation.SuppressLint;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Pattern;

/**
 * @name StringUtils字符串处理工具类
 * @Descripation <br>
 *               1、判断字符串是否为邮件。<br>
 *               2、判断用户名、昵称、密码、年龄、手机号的有效性。<br>
 *               3、空串的检测。<br>
 *               4、获取当前日期(日期型，字符串型)。<br>
 *               5、把当前日期转为两种不同的字符串：2014-09-06，2014-9-6。<br>
 *               6、以友好的形式显示日期。<br>
 * @author 樊俊彬
 * @date 2014-9-17
 * @version 1.0
 */

public class StringUtils {

	private static final String TAG = "StringUtils";
	private final static Pattern emailer = Pattern
			.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");

	private final static ThreadLocal<SimpleDateFormat> dateFormater = new ThreadLocal<SimpleDateFormat>() {
		@Override
		protected SimpleDateFormat initialValue() {
			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		}
	};

	private final static ThreadLocal<SimpleDateFormat> dateFormater2 = new ThreadLocal<SimpleDateFormat>() {
		@Override
		protected SimpleDateFormat initialValue() {
			return new SimpleDateFormat("yyyy-MM-dd");
		}
	};

	/**
	 * 判断给定字符串是否空白串。 <br>
	 * 空白串是指由空格、制表符、回车符、换行符组成的字符串
	 * 
	 * @param input
	 * @return
	 */
	public static boolean isEmpty(String input) {
		if (input == null || "".equals(input))
			return true;

		for (int i = 0; i < input.length(); i++) {
			char c = input.charAt(i);
			if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
				return false;
			}
		}
		return true;
	}

	/**
	 * 判断是不是一个合法的电子邮件地址
	 * 
	 * @param email
	 * @return
	 */
	public static boolean isEmail(String email) {
		if (email == null || email.trim().length() == 0)
			return false;
		return emailer.matcher(email).matches();
	}

	/**
	 * 检测用户名的可用性
	 * 
	 * @param userName
	 * @return
	 */
	public static boolean isUserName(String userName) {
		if (userName == null || userName.length() < 4) {
			return false;
		}
		return true;
	}

	/**
	 * 检测密码的可用性
	 * 
	 * @param password
	 * @return
	 */
	public static boolean isPassword(String password) {
		if (password == null || password.length() < 6) {
			return false;
		}
		return true;
	}

	/**
	 * 检测昵称的可用性
	 * 
	 * @param nickName
	 * @return
	 */
	public static boolean isNickName(String nickName) {
		if (nickName == null || nickName.length() < 2) {
			return false;
		}
		return true;
	}

	/**
	 * 检测年龄的可用性
	 * 
	 * @param ageString
	 * @return
	 */
	public static boolean isAge(String ageString) {
		int age = Integer.valueOf(ageString);
		if (age > 0 && age < 100) {
			return true;
		}
		return false;
	}

	/**
	 * 检测手机号码的可用性
	 * 
	 * @param phone
	 * @return
	 */
	public static boolean isPhone(String phone) {
		if (phone.length() == 11) {
			return true;
		}
		return false;
	}

	/**
	 * 获得当前系统日期"yyyy-mm-dd"
	 * 
	 * @return
	 */
	@SuppressLint("SimpleDateFormat")
	public static String getTodaySimpleDate() {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String todaySimpleDate = simpleDateFormat.format(new java.util.Date());
		return todaySimpleDate;
	}

	/**
	 * 获得当前系统日期"yyyy-m-d"
	 * 
	 * @return
	 */
	public static String getTodatSimpleDate2() {
		StringBuilder sb = new StringBuilder(getTodaySimpleDate());
		if (getTodaySimpleDate().charAt(5) == '0') {
			sb.deleteCharAt(5);
			if (getTodaySimpleDate().charAt(7) == '0') {
				sb.deleteCharAt(7);
			}
		} else if (getTodaySimpleDate().charAt(8) == '0') {
			sb.deleteCharAt(8);
		}
		return sb.toString();
	}

	/**
	 * 判断给定字符串时间是否为今日
	 * 
	 * @param sdate
	 * @return boolean
	 */
	public static boolean isToday1(String date) {
		if (date.equals(getTodaySimpleDate())
				|| date.equals(getTodatSimpleDate2())) {
			return true;
		}
		return false;
	}

	public static boolean isToday2(String sdate) {
		boolean b = false;
		Date time = toDate(sdate);
		Date today = new Date();
		if (time != null) {
			String nowDate = dateFormater2.get().format(today);
			String timeDate = dateFormater2.get().format(time);
			if (nowDate.equals(timeDate)) {
				b = true;
			}
		}
		return b;
	}

	/**
	 * 将字符串转位日期类型
	 * 
	 * @param sdate
	 * @return
	 */
	public static Date toDate(String sdate) {
		try {
			return dateFormater.get().parse(sdate);
		} catch (ParseException e) {
			return null;
		}
	}

	/**
	 * 以友好的方式显示时间
	 * 
	 * @param sdate
	 * @return
	 */
	public static String friendly_time(String sdate) {
		Date time = toDate(sdate);
		if (time == null) {
			return "Unknown";
		}
		String ftime = "";
		Calendar cal = Calendar.getInstance();

		// 判断是否是同一天
		String curDate = dateFormater2.get().format(cal.getTime());
		String paramDate = dateFormater2.get().format(time);
		if (curDate.equals(paramDate)) {
			int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);
			if (hour == 0)
				ftime = Math.max(
						(cal.getTimeInMillis() - time.getTime()) / 60000, 1)
						+ "分钟前";
			else
				ftime = hour + "小时前";
			return ftime;
		}

		long lt = time.getTime() / 86400000;
		long ct = cal.getTimeInMillis() / 86400000;
		int days = (int) (ct - lt);
		if (days == 0) {
			int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);
			if (hour == 0)
				ftime = Math.max(
						(cal.getTimeInMillis() - time.getTime()) / 60000, 1)
						+ "分钟前";
			else
				ftime = hour + "小时前";
		} else if (days == 1) {
			ftime = "昨天";
		} else if (days == 2) {
			ftime = "前天";
		} else if (days > 2 && days <= 10) {
			ftime = days + "天前";
		} else if (days > 10) {
			ftime = dateFormater2.get().format(time);
		}
		return ftime;
	}

	/**
	 * 使用MD5对字符串进行加密
	 * 
	 * @param content
	 * @return
	 */
	public static String getMD5(String content) {
		try {
			MessageDigest digest = MessageDigest.getInstance("MD5");
			digest.update(content.getBytes());

			StringBuilder builder = new StringBuilder();
			for (byte b : digest.digest()) {
				builder.append(Integer.toHexString((b >> 4) & 0xf));
				builder.append(Integer.toHexString(b & 0xf));
			}
			return builder.toString();

		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return null;
	}
}
